package bawei.com.fragment;

import android.os.Bundle;



import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;



import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



import com.squareup.okhttp.Request;


import com.squareup.okhttp.Response;



import java.util.List;

import bawei.com.adapter.To_them_Adapter;

import bawei.com.bilibili.MyAppliction;
import bawei.com.bilibili.R;



import bawei.com.model.BaseCallback;

import bawei.com.model.Network;
import bawei.com.model.OkHttpHelper;


import bawei.com.model.bean.To_them_bean1;


import bawei.com.model.bean.To_them_bean2;


/**
 * 类描述:
 * 作者：cwm
 * 时间:2017/3/9 15:49
 * 邮箱:18310832074@163.com
 */

public class To_themFragment extends Fragment {



    private View view;
    private RecyclerView recyclerView;
    private To_them_Adapter to_them_adapter;
    private To_them_bean1 bean1=new To_them_bean1();
    private To_them_bean2 bean2=new To_them_bean2();
    private SwipeRefreshLayout swipeRefreshLayout;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.to_themfragment,null);
        recyclerView = (RecyclerView) view.findViewById(R.id.to_them_RecyclerView);
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.sRefreshLayout);
        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getRefresh();
        SopCastFragment.flag=false;
        getdate();
    }
    public void  getRefresh(){

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // 停止刷新
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }, 1000); // 2秒后发送消息，停止刷新
            }
        });
        // 设置下拉圆圈上的颜色
        swipeRefreshLayout.setColorSchemeResources(R.color.pink_text_color);
        // 设置手指在屏幕下拉多少距离会触发下拉刷新
        swipeRefreshLayout.setDistanceToTriggerSync(400);
        swipeRefreshLayout.setProgressBackgroundColor(android.R.color.white);
        swipeRefreshLayout.setSize(SwipeRefreshLayout.DEFAULT);
    }

    public void getdate(){

        //番剧轮播及下方数据
        OkHttpHelper.getinstance().get("http://bangumi.bilibili.com/"+Network.DRAMA_DATA, new BaseCallback<To_them_bean1>() {

            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onFailure(Request request, Exception e) {
                Log.i("TAG", "onFailure: ");
            }
            @Override
            public void onSuccess(Response response, To_them_bean1 to_them_bean1) {

                bean1=to_them_bean1;
               Log.i("TAG", "onSuccess: ");
                getdate2();
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                Log.i("TAG", "onError: ");
            }
        });

    }
    public void getdate2(){

        //番剧推荐数据

        OkHttpHelper.getinstance().get("http://bangumi.bilibili.com/"+ Network.DRAMA_RECOMMEND, new BaseCallback<To_them_bean2>() {

            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onFailure(Request request, Exception e) {
                Log.i("TAG", "onFailure2: ");
            }

            @Override
            public void onSuccess(Response response, To_them_bean2 to_them_bean2) {
                bean2=to_them_bean2;
                Log.i("TAG", "onSuccess2: ");
                getbean1();
            }
            @Override
            public void onError(Response response, int errorCode, Exception e) {
                Log.i("TAG", "onError2: "+e);
            }
        });

    }
    public void getbean1(){

        to_them_adapter =new To_them_Adapter(getActivity(),bean1,bean2);
        recyclerView.setAdapter(to_them_adapter);
        GridLayoutManager gridLayoutManager=new GridLayoutManager(getActivity(),12);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

            @Override
            public int getSpanSize(int position) {
                return to_them_adapter.getSpanSize(position);
            }
        });
        recyclerView.setLayoutManager(gridLayoutManager);


    }


}
